<?php
namespace App\Http\Controllers\ApiAdmin;

use App\Service\Admin\UserService;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    //用户

    public function __construct(UserService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $pas['off'] = $request->post('is_off', ''); //0正常，1已注销
        $pas['is_realname'] = $request->post('is_realname', ''); //0未认证，1已认证
        $pas['realname'] = $request->post('realname', ''); //姓名
        $pas['phone'] = $request->post('phone', ''); //手机号
        $res = $this->db->getIndex($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //列表
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['uname'] = $request->post('nick', '');
        $pas['realname'] = $request->post('realname', '');
        $pas['phone'] = $request->post('phone', '');
        $pas['sex'] = $request->post('sex', 0);
        $pas['address_now'] = $request->post('address_now', '');
        $pas['id_card'] = $request->post('id_card', '');
        $pas['head'] = $request->post('head', '');
        $pas['id_front'] = $request->post('id_qian', '');
        $pas['id_back'] = $request->post('id_hou', '');
        $pas['wx_account'] = $request->post('wx_account', '');
        $pas['zfb_account'] = $request->post('zfb_account', '');
        if (!$pas['uname'] || !$pas['realname'] || !$pas['phone']) {
            return $this->toJson(-2, '昵称、姓名或手机号必须的');
        }
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //实名审核
    public function authReal(Request $request)
    {
        $id = $request->post('id', 0);
        $status = $request->post('auth', 0); //1拒绝2通过
        if (!in_array($status, [1, 2])) {
            return $this->toJson(-2, '状态错误');
        }
        $res = $this->db->authReal($id, $status);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //禁用启用
    public function able(Request $request)
    {
        $id = $request->post('id', 0);
        $able = $request->post('able', 0); //0正常1禁用
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->able($id, $able);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}